﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using System.Globalization;
using System.Runtime.Serialization;

namespace Barbar.MapReport.Common {
  [Serializable]
  public class MapReportValidationException : MapReportException {
    /// <summary>
    /// Výchozí konstruktor
    /// </summary>
    public MapReportValidationException() {
    }

    public MapReportValidationException(ValidationResults results)
      : this(GetMessage(results)) {
      foreach (ValidationResult result in results)
        if (result.Key != null)
          Data.Add(result.Key, result);
    }

    /// <summary>
    /// Výchozí konstruktor
    /// </summary>
    /// <param name="message"></param>
    public MapReportValidationException(string message)
      : base(message) {
    }

    /// <summary>
    /// Výchozí konstruktor
    /// </summary>
    /// <param name="message"></param>
    /// <param name="innerException"></param>
    public MapReportValidationException(string message, Exception innerException)
      : base(message, innerException) {
    }


    /// <summary>
    /// Serializařní konstruktor
    /// </summary>
    /// <param name="info"></param>
    /// <param name="context"></param>
    protected MapReportValidationException(SerializationInfo info, StreamingContext context)
      : base(info, context) {
    }

    private static string GetMessage(ValidationResults results) {
      StringBuilder builder = new StringBuilder();
      builder.AppendLine("Validation failed:");
      foreach (ValidationResult result in results) {
        builder.AppendFormat(CultureInfo.InvariantCulture, "Key={0}, Message={1}", result.Key, result.Message);
      }
      return builder.ToString();
    }
  }
}
